package com.wcar.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.CouponCodes;
import com.wcar.common.entity.User;
import com.wcar.common.entity.UserRole;
import com.wcar.common.entity.WashCarCoupon;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.store.dao.CouponDao;
import com.wcar.store.dao.UserRoleDao;
import com.wcar.store.dao.WashCarCouponDao;
import com.wcar.store.service.CouponService;
import com.wcar.store.vo.FrontUserStoreVO;
import com.wcar.store.vo.StoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: four-wcar-idea
 * @BelongsPackage: com.wcar.store.service.impl
 * @Author: 李睢康
 * @CreateTime: 2024-05-28  20:07
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private CouponDao couponDao;
    @Resource
    private WashCarCouponDao washCarCouponDao;
    @Resource
    private UserRoleDao userRoleDao;
    @Override
    public Result<IPage> getAll(StoreVo storeVo) {
        IPage<CouponCodes> iPage = new Page<>(storeVo.getCurrentPage(), storeVo.getPageSize());
        QueryWrapper<CouponCodes> wrapper = new QueryWrapper<>();
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<UserRole> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_id",user.getUserId());
        List<UserRole> userRoles = userRoleDao.selectList(wrapper1);
        ArrayList<Integer> roleIds=new ArrayList<>();
        for (UserRole u: userRoles) {
            roleIds.add(u.getRoleId());
        }
        //判断是不是总店长
        if (!roleIds.contains(1)) {
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            wrapper.eq("tsu.user_id", user.getUserId());
        }
        if (storeVo.getStoreName() != null && storeVo.getStoreName() != "") {
            wrapper.like("ts.store_name", storeVo.getStoreName());
        }
        IPage<CouponCodes> codesIPage = couponDao.selecrGetAll(iPage, wrapper);
        return new Result<>(200, "获取所有优惠卷信息成功", codesIPage);
    }

    @Override
    public Result<Integer> insert(CouponCodes couponCodes) {
        if (couponCodes.getCouponCode().equals("NEW-USER-001")) {
            couponCodes.setCouponType("满减");
        } else if (couponCodes.getCouponCode().equals("RETURN-CASH-002")) {
            couponCodes.setCouponType("返现");
        } else if (couponCodes.getCouponCode().equals("CATEGORY-001")) {
            couponCodes.setCouponType("折扣");
        }
        couponCodes.setUsageCount(0);
        couponCodes.setIsUsed(0);
        Date date = new Date();
        couponCodes.setCreationDate(date);
        int insert = couponDao.insert(couponCodes);
        return new Result<>(200, "添加成功", insert);
    }

    @Override
    public Result<Integer> update(CouponCodes couponCodes) {
        int i = couponDao.updateById(couponCodes);
        return new Result<>(200, "修改优惠卷成功", i);
    }

    @Override
    public Result<Integer> delete(Integer couponId) {
        int i = couponDao.deleteById(couponId);
        return new Result<>(200,"删除成功",i);
    }

    @Override
    public Result<List<WashCarCoupon>> selectCarCoupon(FrontUserStoreVO frontUserStoreVO) {
        QueryWrapper<WashCarCoupon> wrapper=new QueryWrapper<>();
        wrapper.eq("store_id",frontUserStoreVO.getStoreId());
        wrapper.eq("user_id",frontUserStoreVO.getUserId());
        wrapper.eq("is_used",0);
        List<WashCarCoupon> washCarCoupons = washCarCouponDao.selectList(wrapper);
        return new Result<>(200,"查询洗车优惠卷成功",washCarCoupons);
    }
}
